<?php
// 检查是否存在自动登录的Cookie
if (isset($_COOKIE['user_id'])) {
    // 执行自动登录逻辑
    // require __DIR__ . '/../models/init.php';

    $link = db_connect();
    // 防sql注入，处理特殊字符
    $userId = str_handle($link, $_COOKIE['user_id']);

    // 根据id查询用户信息
    $res = mysqli_query($link, "SELECT * FROM `user` WHERE `uid`='{$userId}'");

    while($rs=mysqli_fetch_object($res)) {
        // 通过session来持久化存储用户信息
        // 启动会话
        session_start();

        // 将用户信息存储在会话中
        $_SESSION['user_id'] = $userId;
        $_SESSION['user_name'] = !empty($rs->username) ? $rs->username : null;
        $_SESSION['true_user_name'] = !empty($rs->truename) ? $rs->truename : null;
        $_SESSION['user_email'] = !empty($rs->email) ? $rs->email : null;
        $_SESSION['user_phone'] = !empty($rs->phone) ? $rs->phone : null;
        $_SESSION['user_state'] = !empty($rs->state) ? $rs->state : null;
    }

    mysqli_close($link);
}